package com.hmdp.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.hmdp.dto.UserDTO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//      判断ThreadLocal中是否有user
        if(UserHolder.getUser() == null){
            response.setStatus(401);
            return false;
        }
//      由用户存在，则放行
        return true;
    }

}

//        基于session
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
////        获取session
//        Object userDto = request.getSession().getAttribute("userDto");
//        if(userDto == null){   //判断用户是否存在
//            response.setStatus(401);
//            return false;
//        }
//        UserHolder.saveUser((UserDTO) userDto);
//
//        return true;
//    }